package com.ubxtech.web.locale;

import cn.hutool.core.util.StrUtil;
import com.ubxtech.core.context.ContextConstants;
import com.ubxtech.core.utils.StrPool;
import com.ubxtech.web.properties.AuthProperties;
import com.ubxtech.web.utils.WebUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.LocaleResolver;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author Victor.Xiao
 * @since 2023-08-14 20:13
 **/
@Slf4j
public class CustomLocaleResolver implements LocaleResolver {
    private AuthProperties authProperties;

    public CustomLocaleResolver(AuthProperties authProperties) {
        this.authProperties = authProperties;
    }
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lang = request.getParameter(ContextConstants.HEADER_LANG);
        if (StrUtil.isBlank(lang)) {
            lang = request.getHeader(ContextConstants.HEADER_LANG);
        }
        //Accept-Language,request.getLocale()可能是空,因为request.getLocales()可能有多个值,而且目前只能取到语言部分,不能取到国家
        Locale locale = getLocale(request);
        if(!StrUtil.isEmpty(lang)){
            locale = getLocale(lang);
        }
        //其他情况使用默认值
        if (locale == null && StrUtil.isNotBlank(authProperties.getAcceptLanguage())) {
            locale = getLocale(authProperties.getAcceptLanguage());
        }
        if (locale == null) {
            locale = Locale.getDefault();
        }
        return locale;
    }

    private Locale getLocale(HttpServletRequest request) {
        //直接根据字符串分析
        String lang = WebUtil.getHeader(request, ContextConstants.HEADER_LANGUAGE);
        if (StrUtil.isBlank(lang)) {
            return null;
        }
        return getLocale(lang);
    }

    private Locale getLocale(String lang) {
        if (StrUtil.isBlank(lang)) {
            return null;
        }
        lang = lang.replaceAll(StrPool.UNDERSCORE, StrPool.DASH);
        //先分号分割,zh,zh-CN;q=0.9,en;q=0.8  或者 zh,zh-CN  zh-CN,zh
        String[] localeStrArr = lang.split(StrPool.SEMICOLON);
        for (String localeStr : localeStrArr) {
            //有没有横杠和逗号
            if (!localeStr.contains(StrPool.DASH) || !localeStr.contains(StrPool.COMMA) ) {
                continue;
            }
            //先逗号分割
            String[] countryLangArr = localeStr.split(StrPool.COMMA);
            for (String countryLang : countryLangArr) {
                //有没有-
                if (countryLang.contains(StrPool.DASH)) {
                    String[] locale = countryLang.split(StrPool.DASH);
                    return new Locale(locale[0],locale[1]);
                }
            }
        }

        return null;
    }

    @SuppressWarnings("NullableProblems")
    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }

    public static void main(String[] args) {
        for (Locale availableLocale : Locale.getAvailableLocales()) {
//            System.out.println(availableLocale
//                    +"=====getDisplayName:"+availableLocale.getDisplayName()
//                    +"=====getDisplayCountry:"+availableLocale.getDisplayCountry()
//                    +"=====getDisplayLanguage:"+availableLocale.getDisplayLanguage()
//                    +"=====getDisplayScript:"+availableLocale.getDisplayScript()
//                    +"=====getDisplayVariant:"+availableLocale.getDisplayVariant()
//                    +"=====getISO3Language:"+availableLocale.getISO3Language()
//                    +"=====getCountry:"+availableLocale.getCountry()
//                    +"=====getLanguage:"+availableLocale.getLanguage()
//                    +"=====getVariant:"+availableLocale.getVariant()


            System.out.println(availableLocale+","+availableLocale.getLanguage()+" ====="+availableLocale.getDisplayName()
            );
        }
    }
}
